import qs from 'qs';
import AuthGuard from '../guard/auth-guard';
import { ViewRoute } from './view-route';
import { userIndexRoute } from './user-route';

const appViewRoute = [
	{
		path: '/app/',
		beforeEnter: (to: any, from: any, next: any) => {
			to.defaultPage = true;
			const routerParamsName = 'app';
			const params: any = {};
			if (to.params && to.params[routerParamsName]) {
				Object.assign(
					params,
					qs.parse(to.params[routerParamsName], { delimiter: ';' })
				);
			}
			const url: string = '/appdata';
			const auth: Promise<any> = AuthGuard.getInstance().authGuard(url, params);
			auth
				.then((result: any) => {
					next(result ? true : false);
				})
				.catch(() => {
					next(false);
				});
		},
		component: () =>
			import('@components/app/app-view-shell/app-view-shell.vue'),
		children: [...ViewRoute],
	},
	{
		path: '/login/:login?',
		name: 'login',
		meta: {
			caption: '登录',
			tag: 'AppLoginView',
			requireAuth: false,
			ignoreAddPage: true,
			startPage: false,
		},
		component: () => import('@components/function/login/login.vue'),
	},
	...userIndexRoute,
];

export const AppViewRoute = appViewRoute;
